package com.example.databasedoc.config;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * DataSourceFactory
 *
 * @author lijunsong
 * @date 2021/3/12 18:43
 * @since 1.0
 */
@Component
public class DataSourceFactory {
    @Autowired
    private MyHikariDataSource dataSource;

    public void reload(String username, String password, String url) {
        if (StringUtils.isBlank(url) || StringUtils.isBlank(username) || StringUtils.isBlank(password)) {
            return;
        }
        //加载数据源
        DataSourceReq info = new DataSourceReq();
        info.setUrl(url);
        info.setUsername(username);
        info.setPassword(password);

        dataSource.updateDataSourceMap("1", DataSourceProvider.create(info));
    }
}
